package com.farm.common.security;

import cn.hutool.http.HttpStatus;
import com.farm.common.constant.CommonConstants;
import com.farm.common.util.R;
import lombok.AllArgsConstructor;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@AllArgsConstructor
public class SecurityAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException accessDeniedException){
        R<String> result = new R<>();
        result.setMsg("您的权限不足");
        result.setData(accessDeniedException.getMessage());
        result.setCode(CommonConstants.FAIL);
        SecurityServletResponse.responseProcessing(httpServletRequest, httpServletResponse, result, HttpStatus.HTTP_UNAUTHORIZED);
    }
}
